//
//  CPHttpController.swift
//  CJPackDemo
//
//  Created by 陈警卫 on 2023-12-12.
//

import UIKit
import CJPack

public extension CJHttper {
    
    func firstEntry(_ closure: @escaping CJHttpClosure) {
        requestBase(point: "firstEntry", closure: closure)
    }
}

class CPHttpController: CJBaseBackController {
    
    var tableView: UITableView!
    var json: [String: Any] = [:]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupView()
        setupData()
    }
    
    func setupData() {
        
        kCJ_baseUrl = "http://api.szyoutui.com/"
        kCJ_basePoint = "api/app/"
        
        let hud = CJHuder.showing(toView: view, text: "正在加载")
        CJHttper().firstEntry { [weak hud] config in
            hud?.hide()
            
            refreshView(json: config.json as? [String : Any])
        }
        
        func refreshView(json: [String: Any]?) {
            if let json = json {
                if json.count <= 0 {
                    tableView.cj_setHolderView("没有数据")
                    tableView.cj_mj_setFooter(noMore: true)
                } else {
                    tableView.cj_removeHolderView()
                    tableView.cj_mj_setFooter(noMore: false)
                }
                self.json = json
                self.tableView.reloadData()
            } else {
                tableView.cj_setHolderView("没有数据")
                tableView.cj_mj_setFooter(noMore: true)
            }
        }
        
    }
    
    func setupView() {
        
        let tableView = UITableView(frame: CGRect(x: 0.0,
                                                  y: CJScreen.navBatBarHeight,
                                                  width: view.cj_width,
                                                  height: view.cj_height - CJScreen.navBatBarHeight),
                                    style: .plain)
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        self.tableView = tableView
        
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        
        tableView.cj_mj_headerRefresh { scrollView in
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                tableView.cj_mj_headerEndRefreshing()
            }
        }
        
        tableView.cj_mj_footerRefresh("23424") { scrollView in
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                tableView.cj_mj_footerEndRefreshing()
            }
        }
    }

}

extension CPHttpController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if let datas = json["data"] as? [Any] {
            return datas.count
        } else {
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
        
        if let datas = json["data"] as? [Any],
            let data = datas[indexPath.row] as? [String: Any], 
            let name = data["name"] as? String {
            
            cell.textLabel?.text = name
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
}

